package cn.data.collection.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.data.collection.constant.InitPropertiesConstant;

public class DBConnection {

	public static Connection getConnection() throws SQLException {
		Connection conn = null;
		String dbUser = "";
		String dbUrl = "";
		String dbPwd = "";
		try {
			dbUrl = PropertiesUtils.getInstance().getINITDATA().getProperty(InitPropertiesConstant.DB_URL);
			dbUser = PropertiesUtils.getInstance().getINITDATA().getProperty(InitPropertiesConstant.DB_USER);
			dbPwd = PropertiesUtils.getInstance().getINITDATA().getProperty(InitPropertiesConstant.DB_PWD);

			Class.forName(PropertiesUtils.getInstance().getINITDATA().getProperty(InitPropertiesConstant.CLASS_NAME));
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
			conn.setAutoCommit(true);
		} catch (SQLException se) {
			throw se;
		} catch (Exception e) {
			LoggerUtils.getInstance().error(e);
			throw new SQLException(e.getMessage());
		}
		return conn;
	}

	public static void releaseConnection(Connection conn, Statement stmt,
			ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception localException1) {
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception localException2) {
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
	}

	public static void releaseConnection(Connection conn,
			PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception localException1) {
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (Exception localException2) {
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
			}

		}
	}
	
	public static void main(String[] args) throws Exception {
		PropertiesUtils.getInstance();
		System.out.println(getConnection());
	}

}